﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.SharePoint;
using Hermes.Framework.FileSystem;

namespace Hermes.Framework.FileSystem.SharePoint
{
    public class SharePointFileSystem : INonTransactableFileSystem
    {
        protected SPSite site = null;

        protected SPWeb web = null;

        public SharePointFileSystem(String url)
        {
            if (url == null)
            {
                throw new ArgumentNullException("URL cannot be null.");
            }

            this.site = new SPSite(url);
            this.web = this.site.OpenWeb();
        }

        #region INonTransactableFileSystem Members

        public void Connect(ConnectionInfo connection)
        {
            throw new NotImplementedException();
        }

        public void CreateFile(string fileName)
        {
            throw new NotImplementedException();
        }

        public void Copy(string from, string to)
        {
            FileStream stream = File.Open(from, FileMode.Open);

            SPFileCollection fileCollection = this.web.Files;
            fileCollection.Add(to, stream);

            stream.Close();
        }

        public void Rename(string sourceFileName, string targetFileName)
        {
            SPFile file = this.web.GetFile(sourceFileName);
            file.MoveTo(targetFileName, false);
        }

        public void DeleteFile(string fileName)
        {
            SPFile file = this.web.GetFile(fileName);
            file.Delete();
        }

        public bool Exists(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("File name cannot be null.");
            }

            SPFile file = this.web.GetFile(fileName);

            return file.Exists;
        }

        public void CreateDirectory(string directoryName)
        {
            SPFolderCollection folders = this.web.Folders;
            folders.Add(directoryName);
        }

        public bool IsDirectory(string directoryName)
        {
            SPFolder folder = this.web.GetFolder(directoryName);

            return folder.Exists;
        }

        public void DeleteDirectory(string directoryName)
        {
            SPFolder folder = this.web.GetFolder(directoryName);
            folder.Delete();
        }

        public int GetItemCount(string directoryName)
        {
            throw new NotImplementedException();
        }

        public string GetItem(string directoryName, int index)
        {
            throw new NotImplementedException();
        }

        public void RenameDirectory(string sourceDirectoryName, string targetDirectoryName)
        {
            SPFolder folder = this.web.GetFolder(sourceDirectoryName);
            folder.MoveTo(targetDirectoryName);
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
